<template>
  <div>
    <p>初始：{{ timeArrary }}</p>
    <p>选择：{{ dataTime }}</p>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      dataTime: {},
    };
  },
  computed: {
    // ...mapState(['timeArrary'])
    timeArrary() {
      return this.$store.state.timeArrary;
    },
  },
  watch: {
    timeArrary: {
      handler(a, b) {
        // this.$message(`${a}`);
        this.getTime(a);
        0;
      },
      deep: true, //深度监听
      // immediate: true, //立即监听
    },
  },
  methods: {
    getTime(time) {
      console.log(time, 6843);
      this.dataTime = {
        startTime: time[0].slice(0, 10),
        endTime: time[1].slice(0, 10),
      };

      if (time[2] == "year" || time[2] == "month") {
        let a = new Date().getTime();
        // console.log(a, 3435);
        let b = this.format(a);

        if (time[1].slice(0, 10) > b) {
          // 结尾日期为当日
          this.dataTime.endTime = b;
          //   console.log(this.dataTime, "dataTime");
        }
        // console.log(dataTime,34535);
      } else if (
        this.dataTime.startTime == this.dataTime.endTime &&
        time[2] == "day"
      ) {
        //  如果是日且同一天就减一天
        let data1 = new Date(time[0]).getTime() - 24 * 60 * 60 * 1000;
        let data2 = new Date(time[1]).getTime() - 24 * 60 * 60 * 1000;
        this.dataTime.startTime = this.format(data1);
        this.dataTime.endTime = this.format(data2);
      }
      //   发送请求
    },
    format(a) {
      let c = new Date(a);
      //   console.log(c, 353454564);
      let year = c.getFullYear();
      let month = c.getMonth() + 1;
      let day = c.getDate();
      let hour = c.getHours();
      let minutes = c.getMinutes();
      let second = c.getSeconds();
      let arr = [year, month, day, hour, minutes, second];
      let z = arr.map((r) => {
        // console.log(r);
        if (r < 10) {
          return "0" + r;
        } else {
          return r;
        }
      });
      return z[0] + "-" + z[1] + "-" + z[2];
    },
  },
};
</script>

<style></style>
